#include "TriangleScene.h"
#include "Utils/LogUtil.h"
#include "TriangleEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "TriangleMaterial.h"
#include "Render/RenderState.h"



void TriangleScene::OnInit()
{
	LOGI(L"GameScene Init");

	mCamera.SetLens(0.25f * glm::pi<float>(), (float)GRenderState->GetWidth() / GRenderState->GetHeight(), 0.5f, 100.0f);
	mCamera.UpdateViewMatrix();

	GeometryGenerator geometryGenerator;
	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateSphere(0.1f, 10, 10);
	GeometryGenerator::MeshData cylinderData = geometryGenerator.CreateCylinder(0.1f, 0.03f, 0.4f, 10, 10);
	// GeometryGenerator::MeshData panelData = geometryGenerator.CreateGrid(10.f, 10.f, 10, 10);
	GeometryGenerator::MeshData panelData = geometryGenerator.CreatePanel(10.f, 10.f, glm::vec3(0.f, 1.f, 0.f));
	GeometryGenerator::MeshData cubeData = geometryGenerator.CreateCube(0.2f, 0.2f, 0.2f, 1);


	TriangleEntity* center = CreateEntity<TriangleEntity>();
	center->AddComponent<Model>(cylinderData);
	center->AddComponent<TriangleMaterial>("Blue.png");
	center->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.2f, 0.f));
	center->GetTransform().UpdateMatrices();

	TriangleEntity* pointX = CreateEntity<TriangleEntity>();
	pointX->AddComponent<Model>(cylinderData);
	pointX->AddComponent<TriangleMaterial>("Red.png");
	pointX->GetTransform().SetLocalPosition(glm::vec3(1.f, 0.2f, 0.f));
	pointX->GetTransform().UpdateMatrices();

	TriangleEntity* pointY = CreateEntity<TriangleEntity>();
	pointY->AddComponent<Model>(cylinderData);
	pointY->AddComponent<TriangleMaterial>("Green.png");
	pointY->GetTransform().SetLocalPosition(glm::vec3(0.f, 1.2f, 0.f));
	pointY->GetTransform().UpdateMatrices();

	TriangleEntity* pointZ = CreateEntity<TriangleEntity>();
	pointZ->AddComponent<Model>(cylinderData);
	pointZ->AddComponent<TriangleMaterial>("Yellow.png");
	pointZ->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.2f, 1.f));
	pointZ->GetTransform().UpdateMatrices();

	TriangleEntity* panel = CreateEntity<TriangleEntity>();
	panel->AddComponent<Model>(panelData);
	panel->AddComponent<TriangleMaterial>("Block.png");
	panel->GetTransform().SetLocalPosition(glm::vec3(0.f, 0.f, 0.f));
	panel->GetTransform().UpdateMatrices();


#if 0

	TriangleEntity* sponza = CreateEntity<TriangleEntity>();
	sponza->AddComponent<Model>("Sponza.obj");
	sponza->AddComponent<TriangleMaterial>("Pbr\\Sponza_Albedo.jpg");

#endif


	mLightEntity = CreateEntity<TriangleEntity>();
	mLightEntity->AddComponent<Model>(cubeData);
	mLightEntity->AddComponent<TriangleMaterial>("Blue.png");
	mLightEntity->GetTransform().SetLocalPosition(glm::vec3(0.f, 2.f, 0.f));
	mLightEntity->GetTransform().UpdateMatrices();

	mLightTransformOperation = ImGuizmo::TRANSLATE;
	mLightTransformMode = ImGuizmo::LOCAL;

	mLightTransform = mLightEntity->GetTransform().GetWorldMatrix();

}

void TriangleScene::OnExit()
{

}

void TriangleScene::OnTick(float deltaTime)
{
	// mLightTransform = mLightTransform * glm::scale(glm::mat4(1.f), glm::vec3(-1.f, 0.f, 0.f));
	mLightEntity->GetTransform().SetLocalTransform(mLightTransform);
}

void TriangleScene::OnImgui()
{
	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d", GRenderState->GetFps());

		// ImGui::SliderFloat3("Camera", (float*)&mCamera.mPosition, 0.1f, 1.f);

		if (ImGui::RadioButton("Translate", mLightTransformOperation == ImGuizmo::TRANSLATE))
			mLightTransformOperation = ImGuizmo::TRANSLATE;
		ImGui::SameLine();
		if (ImGui::RadioButton("Rotate", mLightTransformOperation == ImGuizmo::ROTATE))
			mLightTransformOperation = ImGuizmo::ROTATE;
		ImGui::SameLine();
		if (ImGui::RadioButton("Scale", mLightTransformOperation == ImGuizmo::SCALE))
			mLightTransformOperation = ImGuizmo::SCALE;

		if (mLightTransformOperation != ImGuizmo::SCALE)
		{
			if (ImGui::RadioButton("Local", mLightTransformMode == ImGuizmo::LOCAL))
				mLightTransformMode = ImGuizmo::LOCAL;
			ImGui::SameLine();
			if (ImGui::RadioButton("World", mLightTransformMode == ImGuizmo::WORLD))
				mLightTransformMode = ImGuizmo::WORLD;
		}

		ImGui::EndMainMenuBar();
	}

	ImGuizmo::BeginFrame();

	ImGuizmo::SetOrthographic(false);
	// ImGuizmo::AllowAxisFlip(false);
	
	glm::vec3 lightPosition;
	glm::vec3 lightRotation;
	glm::vec3 lightScale;

	ImGuizmo::DecomposeMatrixToComponents(&mLightTransform[0][0], &lightPosition.x, &lightRotation.x, &lightScale.x);

	ImGuizmo::RecomposeMatrixFromComponents(&lightPosition.x, &lightRotation.x, &lightScale.x, &mLightTransform[0][0]);

	ImGuizmo::SetRect(0, 0, GRenderState->GetWidth(), GRenderState->GetHeight());

	ImGuizmo::Manipulate(&mCamera.mZmoView[0][0], &mCamera.mProj[0][0], mLightTransformOperation, mLightTransformMode, &mLightTransform[0][0], NULL, NULL);
}
